package com.yjh.server.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yjh.common.utils.FileUploadUtil;
import com.yjh.pojo.dto.CourtInfoDto;
import com.yjh.pojo.entity.CourtCategory;
import com.yjh.pojo.entity.CourtInfo;
import com.yjh.pojo.vo.CourtInfoVo;
import com.yjh.pojo.vo.R;
import com.yjh.server.service.CourtCategoryService;
import com.yjh.server.service.CourtInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/courtInfo")
public class CourtInfoController {
    @Autowired
    private FileUploadUtil fileUploadUtil;
    @Autowired
    private CourtInfoService courtInfoService;
    @Autowired
    private CourtCategoryService courtCategoryService;

//    用@ModelAttribute接收文本实体，@RequestParam接收文件
    @PostMapping("/add")
    public R<String> commit(@RequestBody CourtInfo courtInfo){

        courtInfoService.save(courtInfo);
        return R.success("添加成功");
    }

    /**
     * 分页显示
     * @param current
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> getByPage(@RequestParam(value="current",defaultValue="1") Integer current,@RequestParam(value="pageSize",defaultValue="4") Integer pageSize){
        //原类型的分页，无分类名称
        Page<CourtInfo> page=new Page<>(current,pageSize);
        courtInfoService.page(page);

        //Vo的分页，忽略records数据
        Page<CourtInfoVo> courtInfoVoPage=new Page<>();
        BeanUtils.copyProperties(page,courtInfoVoPage,"records");

        //改造数据，查询分类名称再塞进Vo里
        List<CourtInfoVo> list = page.getRecords().stream().map(item -> {

            CourtInfoVo courtInfoVo=new CourtInfoVo();
            BeanUtils.copyProperties(item,courtInfoVo);

            CourtCategory courtCategory = courtCategoryService.getById(item.getCategoryId());
            String categoryName = courtCategory.getCategoryName();

            courtInfoVo.setCategoryName(categoryName);

            return courtInfoVo;
        }).collect(Collectors.toList());

        //records数据还回去
        courtInfoVoPage.setRecords(list);

        return R.success(courtInfoVoPage);
    }


    /**
     * 数据更新
     * @param courtInfo
     * @return
     */
    @PutMapping("/update")
    @Transactional
    public R<String> update(@RequestBody CourtInfo courtInfo){
        System.out.println(courtInfo);
        courtInfoService.updateById(courtInfo);

        return R.success("修改成功");
    }

    @DeleteMapping("/del")
    @Transactional
    public R<String> delCourtInfo(@RequestBody List<Integer> ids){

        List<String> delUrls=new ArrayList<>();
        for(Integer id:ids){
            CourtInfo courtInfo = courtInfoService.getById(id);
            String courtImg = courtInfo.getCourtImg();
            String[] urls = courtImg.split(",");
            for (String url:urls){
                delUrls.add(url);
            }
        }
        fileUploadUtil.deleteImgsByUrls(delUrls);
        courtInfoService.removeByIds(ids);
        return R.success("删除成功");
    }
}
